###############################################################################
# Get XML files from Kiva servers and store them locally
# 
# Created by Jon Heller (jonearles@yahoo.com)
###############################################################################


#All files will be stored in subfolders in this directory.
my $baseDirectory = "D:/Kiva/";


#Create user agent
use strict;
use LWP::UserAgent;
my $userAgent = LWP::UserAgent->new;
$userAgent->agent("test");



#Find the latest loan ID from this page and set it as the second parameter:
#  http://api.kivaws.org/v1/loans/newest.xml
loans(0, 100000);



partners();


###############################################################################
# Loans
# Input 1: Starting ID
# Input 2: Ending ID
#
# Gets loans in chunks of 10.
###############################################################################
sub loans
{
  my $loanDirectory = $baseDirectory."xml/loans/";
  my $loanID;
  my $loanIDString;

  #Get 10 loans at once for faster download
  for ($loanID = $_[0]; $loanID <= $_[1]; )
  {
    my $loanIDString = "";

    for(my $i = 0; $i <= 9; $i++)
    {
      $loanIDString = $loanIDString.",".$loanID;
      $loanID++;
    }
    $loanIDString = substr($loanIDString, 1);

    #Create request
    my $request = HTTP::Request->new(GET => 'http://api.kivaws.org/v1/loans/'.$loanIDString.'.xml');

    #pass request to User Agent and get a response
    my $response = $userAgent->request($request);

    #Don't do anything if the $response is an error
    if (substr($response->content, 39, 7) ne '<error>')
    {
      open MYFILE, ">".$loanDirectory.($loanID-10).".xml" or die $!;
      print MYFILE $response->content;
      close (MYFILE); 
    }
  }
}



###############################################################################
# Partners (a single file)
###############################################################################
sub partners
{
  my $partnerDirectory = $baseDirectory."xml/";
  my $request = HTTP::Request->new(GET => "http://api.kivaws.org/v1/partners.xml");
  my $response = $userAgent->request($request);
  open MYFILE, ">", $partnerDirectory."partners.xml" or die $!;
  print MYFILE $response->content;
  close (MYFILE); 
}
